package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.widget.Tools;

import org.json.JSONException;
import org.json.JSONObject;

public class FreeWayStateShowActivity extends JTActivity implements View.OnClickListener {

    private String message;

    private LinearLayout lin_freeway_total,lin_freeway_show;

    private LinearLayout lin_freeway_show_meilage, lin_freeway_show_origin, lin_freeway_show_destination, lin_freeway_show_station;

    private TextView order_id, order_time, order_meilage, order_origin, order_destination, order_station;

    private ImageView bcak;

    private TextView freeway_name, station_name;

    private String[] names;

    private JSONObject mJSONObject;

    private String id = "", start_time = "", end_time = "", meilage = "", origin = "", destination = "", station = "", price = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_freeway_state);

        message = getIntent().getStringExtra("message");

        try {

            mJSONObject = new JSONObject(getIntent().getStringExtra("value"));

        } catch (JSONException e) {

            e.printStackTrace();

        }

        id = mJSONObject.optString("order_num");

        start_time = mJSONObject.optString("in_time");

        end_time = mJSONObject.optString("out_time");

        meilage = mJSONObject.optString("mileage");

        origin = mJSONObject.optString("in_name");

        destination = mJSONObject.optString("out_name");

        station = mJSONObject.optString("station");

        price = mJSONObject.optString("price");

        initView();
    }

    private void initView() {

        (bcak = (ImageView) findViewById(R.id.success_close)).setOnClickListener(this);

        (lin_freeway_total = (LinearLayout) findViewById(R.id.lin_freeway_total)).setOnClickListener(this);

        lin_freeway_show = (LinearLayout) findViewById(R.id.lin_freeway_show);

        lin_freeway_show_meilage = (LinearLayout) findViewById(R.id.lin_freeway_show_meilage);

        lin_freeway_show_origin = (LinearLayout) findViewById(R.id.lin_freeway_show_origin);

        lin_freeway_show_destination = (LinearLayout) findViewById(R.id.lin_freeway_show_destination);

        lin_freeway_show_station = (LinearLayout) findViewById(R.id.lin_freeway_show_station);

        order_id = (TextView) findViewById(R.id.tv_freeway_show_order_id);

        order_time = (TextView) findViewById(R.id.tv_freeway_show_order_time);

        order_meilage = (TextView) findViewById(R.id.tv_freeway_show_order_meilage);

        order_origin = (TextView) findViewById(R.id.tv_freeway_show_order_start_point);

        order_destination = (TextView) findViewById(R.id.tv_freeway_show_order_end_point);

        order_station = (TextView) findViewById(R.id.tv_freeway_show_order_station);

        freeway_name = (TextView) findViewById(R.id.tv_freeway_name);

        station_name = (TextView) findViewById(R.id.tv_station_name);

        names = message.split(",");

        if (names[0].equals("欢迎驶入")) {

            lin_freeway_show.setBackgroundResource(R.mipmap.freeway_show_in);

            lin_freeway_show_meilage.setVisibility(View.GONE);

            lin_freeway_show_destination.setVisibility(View.GONE);

            lin_freeway_show_station.setVisibility(View.GONE);

            order_time.setText(start_time.substring(0, 16) + "   ---   ???");

        } else {

            lin_freeway_show.setBackgroundResource(R.mipmap.freeway_show_out);

            order_meilage.setText(Tools.F_num(meilage) + "公里");

            order_destination.setText(destination);

            order_station.setText("共消费" + Tools.F_num(price) + "元");

            order_time.setText(start_time.substring(0, 16) + "  ---  " + end_time.substring(0, 16));

        }

        freeway_name.setText(names[1]);

        station_name.setText(names[2]);

        order_id.setText("行程编号：" + id);

        order_origin.setText(origin);

        if(destination.equals("")||destination.equals(null)){

            lin_freeway_show_destination.setVisibility(View.GONE);

        }else {

            order_destination.setText(destination);

        }

        if(station.equals("")||station.equals(null)||price.equals(null)||price.equals(null)){

            lin_freeway_show_station.setVisibility(View.GONE);

        }else {

            order_station.setText("途径" + Tools.F_num(station) + "个收费站，共消费" + Tools.F_num(price) + "元");

        }

    }

    @Override
    public void onClick(View view) {

        super.onClick(view);

        switch (view.getId()) {

            case R.id.success_close:

                quit();

                break;

            case R.id.lin_freeway_total:

                JTApplication.INSTANCE.finishActivity(FreeWayDetailAcitity.class, FreeWayOrderInfoActivity.class);

                if (names[0].equals("欢迎驶入")) {

                    startActivity(new Intent(context, FreeWayOrderInfoActivity.class));

                    finish();

                } else {

                    startActivity(new Intent(context, FreeWayDetailAcitity.class)

                            .putExtra("id", id)

                            .putExtra("meilage", meilage)

                            .putExtra("time", (Long.parseLong(Tools.dataFive(end_time))-Long.parseLong(Tools.dataFive(start_time)))/60)

                            .putExtra("station", "2")

                            .putExtra("flag","1")

                    );

                    finish();

                }

        }

    }

    private void quit() {

        if (JTApplication.INSTANCE.getActivityStackSize() == 1) {

            startActivity(new Intent(FreeWayStateShowActivity.this, FreeWayOrderInfoActivity.class));

            finish();

        } else {

            finish();

        }

    }
}
